from dvadmin.utils.serializers import CustomModelSerializer
from pdms.models import pumpModel
from django_filters.rest_framework import FilterSet
from dvadmin.utils.viewset import CustomModelViewSet


# ================================================= #
# ************** 客户 序列化器  ************** #
# ================================================= #
class PumpModelSerializer(CustomModelSerializer):
    class Meta:
        model = pumpModel
        fields = '__all__'


class PumpModelCreateUpdateSerializer(CustomModelSerializer):
    """
    创建/更新时的列化器
    """

    # 此处可写定制的 创建/更新 内容
    def validate(self, attrs: dict):
        return super().validate(attrs)

    class Meta:
        model = pumpModel
        fields = '__all__'


# ================================================= #
# ************** 客户 过滤器  ************** #
# ================================================= #
class PumpModelFilter(FilterSet):
    # 客户库 简单序过滤器
    # 通过 lookup_expr 可进行模糊查询，其他配置可自行百度
    # power = CharFilter(lookup_expr='icontains')

    class Meta:
        model = pumpModel
        exclude = ('description',)


class PumpModelViewSet(CustomModelViewSet):
    queryset = pumpModel.objects.all()
    serializer_class = PumpModelSerializer  # 序列化器
    create_serializer_class = PumpModelCreateUpdateSerializer  # 创建/更新时的列化器
    update_serializer_class = PumpModelCreateUpdateSerializer  # 创建/更新时的列化器
    filter_class = PumpModelFilter  # 过滤器
    search_fields = ('type',)  # 搜索
    ordering = ['-power']  # 默认排序


